var insert = function(intervals, newInterval) {
    let arr = []
    let flag = true
    for (let i = 0; i < intervals.length; i++) {
        const item = intervals[i]
        if (item[0] >= newInterval[0] && flag) {
            arr.push(newInterval)
            arr.push(item)
            flag = false
        } else {
            arr.push(item)
        }
    }
    if (flag) {
        arr.push(newInterval)
    }
    console.log(arr)
    let rsl = [arr[0]]
    for (let i = 1; i < arr.length; i++) {
        const item = arr[i]
        let current = rsl[rsl.length - 1]
        if (item[0] <= current[1]) {
            rsl.pop()
            rsl.push([current[0], Math.max(item[1], current[1])])
        } else {
            rsl.push(item)
        }
    }
    return rsl
};
console.log(insert([
    [1, 2],
    [3, 5],
    [6, 7],
    [8, 10],
    [12, 16]
], [4, 8]))